package com.reviewit.util;

import java.security.MessageDigest;

import org.apache.commons.codec.binary.Hex;

public class CryptoUtil {
	private static MessageDigest md5;
	private static MessageDigest sha256;

	static {
		try {
			md5 = MessageDigest.getInstance("md5");
			sha256 = MessageDigest.getInstance("SHA-256");
		} catch (Exception e) {
		}
	}

	public static String toMD5(String str) {
		try {
			byte[] bytes = str.getBytes("UTF-8");
			bytes = md5.digest(bytes);
			return new String(Hex.encodeHex(bytes));
		} catch (Exception e) {
			return null;
		}
	}

	public static String toSHA256(String str) {
		try {
			byte[] bytes = str.getBytes("UTF-8");
			bytes = sha256.digest(bytes);
			return new String(Hex.encodeHex(bytes));
		} catch (Exception e) {
			return null;
		}
	}
}